﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Volume_Resource_Management
{
    public partial class frmUpdate : Form
    {

        string storeNumber;
        frmMain mainForm;

        public frmUpdate(string sNo, frmMain main)
        {
            InitializeComponent();
            storeNumber = sNo;
            mainForm = main;
        }

        private void frmUpdate_Load(object sender, EventArgs e)
        {
            this.Text = "Information Update - Store " + storeNumber;

            button1.Left = (groupBox1.Width - button1.Width) / 2;
            button2.Left = (groupBox2.Width - button2.Width) / 2;
            button3.Left = (this.ClientSize.Width - button3.Width) / 2;

            progressBar1.Maximum = databaseAccessability.getCurrentGFPValues(Convert.ToInt32(storeNumber));
            progressBar2.Maximum = databaseAccessability.getCurrentASValues(Convert.ToInt32(storeNumber));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Open Text File";
            openFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog1.Filter = "TXT Files (*.txt)|*.txt";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                int count = 0;
                string filename = openFileDialog1.FileName;
                StreamReader reader = new StreamReader(filename);
                textBox1.Text = openFileDialog1.FileName;
                bool error = false;

                string import = reader.ReadLine();

                while ((import = reader.ReadLine()) != null)
                {
                    string[] split = import.Split('^');

                    if (split.Length == 10)
                    {
                        if (split[1] == "STO" + storeNumber)
                            count++;
                        else
                        {
                            MessageBox.Show(this, "The Current File Does Not Match Your Store Number");
                            error = true;
                            break;
                        }
                    }
                    else
                    {
                        error = true;
                        break;
                    }
                }

                reader.Close();

                if (error)
                {
                    MessageBox.Show(this, "Error With File, Please Make Sure Delimiter Set To ^");
                    textBox1.Text = "";
                }
                else
                {
                    progressBar1.Maximum += count;
                    button1.Enabled = false;

                    if (button2.Enabled == false)
                        button3.Enabled = true;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string errorMessage = "";

            openFileDialog2.Title = "Open Text File";
            openFileDialog2.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog2.Filter = "TXT Files (*.txt)|*.txt";
            openFileDialog2.FilterIndex = 2;
            openFileDialog2.RestoreDirectory = true;

            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                int count = 0;
                string filename = openFileDialog2.FileName;
                StreamReader reader = new StreamReader(filename);
                textBox2.Text = openFileDialog2.FileName;
                bool error = false;

                string import = reader.ReadLine();

                while ((import = reader.ReadLine()) != null)
                {
                    string[] split = import.Split('^');

                    if (split.Length == 12)
                    {
                        if (split[0] == "STO" + storeNumber)
                            count++;
                        else
                        {
                            MessageBox.Show(this, "The Current File Does Not Match Your Store Number");
                            error = true;
                            errorMessage = "The Current File Does Not Match Your Store Number";
                            break;
                        }
                    }
                    else
                    {
                        error = true;
                        errorMessage = "Error With File. The File May Not Be In The Correct Format";
                        break;
                    }

                    if (button1.Enabled == false)
                        button3.Enabled = true;
                }

                reader.Close();

                if (error)
                {
                    MessageBox.Show(this, errorMessage);
                    textBox2.Text = "";
                }
                else
                {
                    progressBar2.Maximum += count;
                    button2.Enabled = false;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3.Enabled = false;

            Thread loading = new Thread(runUpdate);
            loading.IsBackground = true;
            loading.Start();
        }

        public void runUpdate()
        {
            databaseAccessability.truncateGFPDatabase(Convert.ToInt32(storeNumber), this);
            
            StreamReader reader = new StreamReader(textBox1.Text);
            String import = reader.ReadLine();

            while ((import = reader.ReadLine()) != null)
            {
                string[] split = import.Split('^');
                databaseAccessability.uploadNewGFPDatabase(this, split[0], split[1], split[2], split[3], split[4], split[5],
                    split[6], split[7], split[8], split[9]);
            }
            reader.Close();

            databaseAccessability.truncateASDatabase(Convert.ToInt32(storeNumber), this);

            StreamReader reader2 = new StreamReader(textBox2.Text);
            string import2 = reader2.ReadLine();

            while ((import2 = reader2.ReadLine()) != null)
            {
                string[] split2 = import2.Split('^');
                databaseAccessability.uploadNewASDatabase(this,split2[0], split2[1], split2[3], split2[4], split2[5], split2[6], split2[7], split2[8], split2[9], split2[10],
                    split2[11]);
            }
            reader2.Close();

            this.Invoke(new Action(() => mainForm.Hide()));
            frmSplash splash = new frmSplash();
            this.Invoke(new Action(() => splash.Show()));
            this.Invoke(new Action(() => this.Hide()));
        }
    }
}
